﻿function validatePass(p1, p2)
{
    if (p1.value != p2.value || p1.value == '' || p2.value == '')
    {
        p2.setCustomValidity('Пароли не совпадают');
    } else
    {
        p2.setCustomValidity('');
    }
}
function deleteGroup(id)
{
    doAjaxDisableUser("Admin", "DeleteGroup", { id: id });
    $('#groupId_' + id).remove();
}
function addParentOpen()
{
    var model = {};
    $("#currentParentsDiv > p").each(function(i, v)
    {
        model["ids[" + i + "]"] = $(v).attr("data-idparent");
    });
    model["groupId"] = $("#groupId").val();
    //console.log(model);
    doAjaxHtml("Admin", "GetAvailableParents", model, function (data) { showModal("tmpParentList", true); }, "#tmpParentList");
    
    //$("#availableParentList").selectedIndex = 2;
    
    console.log($("#availableParentList"));
    
}
function addParent() {
    var newId = $("#availableParentList").attr("id");
    var newGroupId = $("#availableParentList").val();
    //console.log($("#availableParentList"));
    var newText = $("#availableParentList option:selected").text();
    
    /*$("#currentParentsDiv").append(
        '<div class="fleft currentParentItem" data-idParent="' + newId + '">' + newText + '</div>');*/

    var id = $('#groupId').val();
    
    console.log("Параметры в AddParentForGroup() --- pIdParent: " + newGroupId + ", pIdGroup: " + id);
    
    doAjaxDisableUser("Admin", "AddParentForGroup", { pIdParent: newGroupId, pIdGroup: id });
    
    $("#currentParentsDiv").append(
        "<p data-idparent='" + newGroupId + "'><code class='lead' style='color:black'>" + newText + "</code><i class='icon-remove-sign' onclick='delParent(" + newGroupId + ");'></i></p>");

    closeModal(false);
}
function addGroupField()
{
    doAjaxHtml("Admin", "GetGroupFieldRow", null, function()
    {
        $("#lastRow2").before($("#tmpParentList").html());
        $("#tmpParentList").html("");
    }, "#tmpParentList");
}
function deleteGroupField(e)
{
    e.closest("tr").remove();
}
function fieldTypeChange(e)
{
    var currRow = e.closest("tr");
    console.log(currRow.find(".fieldVariant").length);
    if (currRow.find(".fieldVariant").length == 0 && (e.val() == 5 || e.val() == 4))
    {
        e.after('<textarea type="text" value="" class="fieldVariant" pattern="[A-Za-zА-Яа-яЁё0-9\s]{1,}+;"></textarea>');
    }
    if (!(e.val() == 4 || e.val() == 5))
    {
        console.log('q');
        currRow.find(".fieldVariant").remove();
    }
}
function SaveGroup()
{
    var model = {};
    model["Name"] = $("#groupName").val();
    model["Id"] = $("#groupId").val();
    $("#currentParentsDiv > div").each(function (i, v)
    {
        model["SelectedParents[" + i + "].Key"] = $(v).attr("data-idparent");
        model["SelectedParents[" + i + "].Value"] = $(v).text();
    });
    $(".ParentGroupField").each(function(i, v)
    {
        model["Fields[" + i + "].Name"] = $(v).find(".fieldName").val();
        var vType = $(v).find(".fieldType").val();
        model["Fields[" + i + "].Type"] = vType;
        console.log($(v).find(".fieldName").val());
        if (vType == 5 || vType == 4)
        {
            $($(v).find(".fieldVariant").val().split(";")).each(function (index2, value2)
            {
                console.log("q! "+index2 + " " + value2);
                model["Fields[" + i + "].AvaliableValues["+index2+"].Value"] = value2;
                model["Fields[" + i + "].AvaliableValues["+index2+"].Key"] = index2;
            });
        }
        model["Fields[" + i + "].UserCanEdit"] = $(v).find(".fieldCanEdit").prop("checked");
        model["Fields[" + i + "].Mandatory"] = $(v).find(".fieldMandatory").prop("checked");
    });
    console.log(model);
    doAjax("Admin", "UpdateGroup", model, function()
    {
        $("#resultSpan").html("<span style='color:green;font-weight: bold;' >success</span>");
        setTimeout("$('#resultSpan').html('');", 3000);

    }, function()
    {
        $("#resultSpan").html("<span style='color:red;font-weight: bold;'>fail</span>");
        setTimeout("$('#resultSpan').html('');", 3000);

    });
}


function change_func() {
    //$('#availableParentList').val();
    /*var sel = document.getElementById("availableParentList");
    var txt = sel.options[sel.selectedIndex].text;
    alert('bpvtybkcz ' + txt);*/
    
    var newGroupId = $("#availableParentList").val();
    var newText = $("#availableParentList option:selected").text();
    console.log(newText + " " + newGroupId);
}

function delParent(e,newGroupId) {
    
    var id = $('#groupId').val();

    //doAjaxDisableUser("Admin", "DelParentForGroup", { pIdParent: newGroupId, pIdGroup: id });
    doAjax("Admin", "DelParentForGroup",  { pIdParent: newGroupId, pIdGroup: id });
    e.closest("p").remove();
}
